#encoding:utf-8

from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from django.db import models
from common.models import Address, Area, Industry, Gender, Employment_Type, Degree
from time import time
from django.contrib.localflavor.us.models import PhoneNumberField
from countries_states.models import State, Municipal
from datetime import date


# Area de empresas
class Company_Area(models.Model):
    name = models.CharField(max_length=150)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering=['name']


class Company_Industry(models.Model):
    name = models.CharField(max_length=150)
    company_area = models.ForeignKey(Company_Area)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering=['name']


def get_upload_file_name(instance, filename):
    return "logos/%s_%s" % (str(time()).replace('.','_'), filename)


class Company(models.Model):
    user = models.OneToOneField(User)
    company_name = models.CharField(max_length=200)
    company_area = models.ForeignKey(Company_Area)
    company_industry = models.ForeignKey(Company_Industry)
    address = models.OneToOneField(Address)
    phone = PhoneNumberField()
    url = models.URLField("Website", blank=True, null=True)
    logo = models.ImageField(upload_to=get_upload_file_name, default='logos/default.jpg', blank=True)

    def __unicode__(self):
        return self.company_name


# Area de vacantes
class VacancyStatus(models.Model):
    """
    Vacancy´s status
    id - codename´s:
        1 - active
        2 - inactive
        3 - programmed
        4 - finalized
        5 - removed
    """
    name = models.CharField(max_length=30)
    codename = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

class Employment_Experience(models.Model):
    """
    La experiencia requerida para una vacante
    """
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

class Salary_Type(models.Model):
    """
    Tipo de salario ofrecido para cada vacante
    """
    name = models.CharField(max_length=30)
    codename = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name


def get_ages():
    ages = [(0, _('Indistinto'))]
    for age in range(18, 70, 1):
        ages.append([str(age), str(age)])
    return ages


class Vacancy(models.Model):
    company = models.ForeignKey(Company)
    user = models.ForeignKey(User)
    status = models.ForeignKey(VacancyStatus, default='inactive')
    employment = models.CharField(max_length=120)
    description = models.TextField()
    state = models.ForeignKey(State, on_delete=models.SET_NULL, null=True)
    municipal = models.ForeignKey(Municipal, on_delete=models.SET_NULL, null=True)
    area = models.ForeignKey(Area, null=True, default=None, blank=True, on_delete=models.SET_NULL)
    industry = models.ForeignKey(Industry, null=True, blank=True, on_delete=models.SET_NULL)
    gender = models.ForeignKey(Gender, null=True, on_delete=models.SET_NULL)
    employmentType = models.ForeignKey(Employment_Type, null=True, on_delete=models.SET_NULL)
    employmentExperience = models.ForeignKey(Employment_Experience,null=True, on_delete=models.SET_NULL)
    degree = models.ForeignKey(Degree, null=True, on_delete=models.SET_NULL)
    min_age = models.CharField(choices=get_ages(), null=True, blank=True, default=None, max_length=2)
    max_age = models.CharField(choices=get_ages(), null=True, blank=True, default=None, max_length=2)
    salaryType = models.ForeignKey(Salary_Type, null=True, on_delete=models.SET_NULL, default=None)
    salary = models.CharField(max_length=6, null=True, blank=True, default=None)
    pub_after = models.BooleanField(verbose_name=_(u'¿Publicar a fecha futura?'))
    add_date = models.DateTimeField(verbose_name=_(u'Fecha de alta'),
                                    auto_now_add=True)
    pub_date = models.DateTimeField(verbose_name=_(u'Fecha de publicación'),
                                    default=date.today,
    )
    end_date = models.DateTimeField(verbose_name=_(u'Fecha de finalización'),
    )
    last_modified = models.DateTimeField(
        verbose_name=_(u'Ultima modificación'),
        auto_now=True
    )

    class Meta:
        verbose_name = _(u'Vacante')
        verbose_name_plural = _(u'Vacantes')
        ordering = ('-pub_date', '-id')

    def __unicode__(self):
        return self.employment